package com.lumm.controller;

import com.lumm.model.ResponseObject;
import com.lumm.model.Staff;
import com.lumm.model.Test;
import com.lumm.services.AdminService;
import com.lumm.util.FileUtil;
import com.lumm.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

@Controller
@RequestMapping("/admin")

public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping(value = {"", "/", "index.html"})
    public String index(HttpSession session, Model model) {
        Test test;

        System.out.println("index");

        test = new Test();


        if (session.getAttribute("staffLogin") == null)
            return "admin/login";

        Staff staff = (Staff) session.getAttribute("staff");

        staff.setPassword("");

        model.addAttribute("staff", staff);

        return "admin/admin";
    }


    @RequestMapping("query")
    public String query(@RequestParam(value = "username", required = true) String username, Model model) {
        return "test";
    }

    @ResponseBody
    @RequestMapping("encryptPassword")
    public ResponseObject encryptPassword(@RequestParam(value = "password") String password) {
        return new ResponseObject(PasswordUtil.encryptPassword(password));
    }


    @RequestMapping(value = "addStaff", method = RequestMethod.POST)
    public String addStaff(
            @RequestParam(value = "staffId", required = true) int staffId,
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "gender", required = true) String gender,
            @RequestParam(value = "email", required = true) String email,
            @RequestParam(value = "type", required = true) short type,
            @RequestParam(value = "password", required = true) String password,
            HttpServletResponse httpServletResponse,
            HttpSession session,
            Model model
    ) {
        Staff staff = (Staff) session.getAttribute("staff");
        if (staff.getType() != 0)
            return "redirect:/admin";

        if (adminService.getStaffByStaffId(staffId)!=null){
            model.addAttribute("msg","员工号已存在！");
            return "alert";
        }

        Staff newStaff = adminService.createNewStaffInfo(staffId, name, password, email, gender, type);
        ArrayList<Staff> staffList = new ArrayList<>();
        staffList.add(newStaff);
        model.addAttribute("staffList", staffList);
        return "admin/htmlGenerators/generateStaffList";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password,
            HttpSession session,
            Model model
    ) {
        Staff staff = adminService.login(username, password);
        if (staff == null){
            model.addAttribute("msg","用户名或密码错误！");
            return "admin/login";
        }
        else {
            session.setAttribute("staffLogin", "1");
            session.setAttribute("staff", staff);
            return "redirect:/admin";
        }
    }


    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout(HttpSession session) {
        session.removeAttribute("staffLogin");
        session.removeAttribute("staff");
        return "redirect:/admin";
    }

    @RequestMapping("loginSuccess")
    public String loginSuccess(HttpSession session) {
        Test test;

        System.out.println(session.getAttribute("staffLogin"));

        if (session.getAttribute("staffLogin") == null)
            return "redirect:/admin/index.html";
            //test = testService.querybyUsername(username);
        else return "redirect:/admin/admin.jsp";
    }


    @RequestMapping(value = "getStaffList", method = RequestMethod.GET)
    public String getStaffList(
            @RequestParam(value = "offset", required = false, defaultValue = "0") int offset,
            @RequestParam(value = "count", required = false, defaultValue = "20") int count,
            HttpServletResponse httpServletResponse,
            HttpSession session,
            Model model
    ) {
        Staff staff = (Staff) session.getAttribute("staff");
        if (staff.getType() != 0)
            return "redirect:/admin";
        model.addAttribute("staffList", adminService.getStaffList(offset, count));
        return "admin/htmlGenerators/generateStaffList";
    }

    @ResponseBody
    @RequestMapping(value = "deleteStaff", method = RequestMethod.GET)
    public ResponseObject deleteStaff(
            @RequestParam(value = "staffId", required = true) int staffId,
            HttpSession session
    ) {
        Staff staff = (Staff) session.getAttribute("staff");
        if (staff.getType() != 0)
            return new ResponseObject(1,-2);
        return new ResponseObject(1,adminService.deleteStaffInfo(staffId));
    }

    @ResponseBody
    @RequestMapping(value = "updateStaffInfo", method = RequestMethod.POST)
    public ResponseObject updateStaffInfo(
            @RequestParam(value = "staffId", required = true) int staffId,
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "gender", required = true) String gender,
            @RequestParam(value = "type", required = true) short type,
            @RequestParam(value = "password",required = true) String password,
            @RequestParam(value = "email",required = false) String email,
            HttpSession session
    ) {
        Staff staff = (Staff) session.getAttribute("staff");
        if (staff.getType() != 0)
            return new ResponseObject(0,-2);

        int status = adminService.updateStaffInfo(staffId, name, password, gender, type);

        if (status==0)
            return new ResponseObject(0,3);

        return new ResponseObject(1,1,adminService.getStaffByStaffId(staffId));
    }


    @ResponseBody
    @RequestMapping(value = "getStaffCount", method = RequestMethod.GET)
    public ResponseObject getStaffCount(
            HttpServletResponse httpServletResponse,
            HttpSession session,
            Model model
    ) {
        //model.addAttribute("staffList", adminService.getStaffList(offset, count));
        return new ResponseObject(1,1,adminService.getStaffCount());
    }











}
